package com.jerry.constellation.Adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jerry.constellation.R;
import com.jerry.constellation.Utils.AssetsUtils;
import com.jerry.constellation.bean.StarInfoBean;

import java.util.List;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;

public class StarBaseAdapter extends BaseAdapter {
    Context mcontext;
    private List<StarInfoBean.StarinfoBean> mdata;
    Map<String, Bitmap> logoMap;

    public StarBaseAdapter(Context context,List<StarInfoBean.StarinfoBean> data) {
        this.mcontext = context;
        this.mdata = data;
        logoMap = AssetsUtils.getLogoImgMap();
    }

    @Override
    public int getCount() {
        return mdata.size();
    }

    @Override
    public Object getItem(int i) {
        return mdata.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {
            view = LayoutInflater.from(mcontext).inflate(R.layout.starfrg_gridview_item, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();

        }
//获取指定位置的数据
        StarInfoBean.StarinfoBean Bean = mdata.get(i);
        viewHolder.textView.setText(Bean.getName());
        //获得图片名称，根据名称在内存中进行查找
        String logoname = Bean.getLogoname();
        Bitmap bitmap = logoMap.get(logoname);
        viewHolder.circleImageView.setImageBitmap(bitmap);
        return view;
    }

    //对于item中的组件进行初始化
    class ViewHolder {
        CircleImageView circleImageView;
        TextView textView;

        public ViewHolder(View view) {
            circleImageView = view.findViewById(R.id.profile_image);
            textView = view.findViewById(R.id.item_star_tv);
        }
    }
}
